<?php
/**
 ***********************************************************************************************************************
 * session生成
 ***********************************************************************************************************************
 */
namespace util\session;
/**
 * Class session
 * @package service
 */
class session
{
    /**
     * 创建session信息
     * @param $user_id
     * @return array
     */
    public static function create($prefix = '', $expire = 1296000)
    {
        $session_id = create_pwd($prefix . uniqid(microtime()));
        $session_salt = create_pwd_salt();
        return array(
            'session_id' => $session_id,
            'session_salt' => $session_salt,
            'refresh_session_id' => create_pwd($session_id, $session_salt),
            'expire_at' => date('Y-m-d H:i:s', time() + intval($expire))
        );

    }

    /**
     * 验证refresh_session_id
     * @param $session_id
     * @param $session_salt
     * @param $refresh_session_id
     * @return bool
     */
    public static function verify($session_id, $session_salt, $refresh_session_id)
    {
        if(empty($session_id) || empty($refresh_session_id) || empty($session_salt)) {
            return false;
        }
        return $refresh_session_id == create_pwd($session_id, $session_salt);
    }
}